<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="SpringCloud_2黑马, Lemonade&#39;s Blog">
    <meta name="description" content="
黑马程序员


常用总结与问题解决
常用总结
问题解决


服务框架学习路线
springcloud实用篇一
一、认识微服务
服务框架演变
微服务技术对比
SpringCloud


二、微服务拆分案例
服务拆分
案例cloud-demo">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>SpringCloud_2黑马 | Lemonade&#39;s Blog</title>
    <link rel="icon" type="image/png" href="https://notes2021.oss-cn-beijing.aliyuncs.com/2021/IMG_0551.JPG">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.0.0"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <span class="logo-span">Lemonade&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <div class="logo-name">Lemonade&#39;s Blog</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/10.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">SpringCloud_2黑马</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/spring/">
                                <span class="chip bg-color">spring</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/3-%E5%88%86%E5%B8%83%E5%BC%8F-%E5%BE%AE%E6%9C%8D%E5%8A%A1/" class="post-category">
                                3.分布式&微服务
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-01-01
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-01-15
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    8.5k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    35 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<p>黑马程序员</p>
</blockquote>
<ul>
<li><a href="#%E5%B8%B8%E7%94%A8%E6%80%BB%E7%BB%93%E4%B8%8E%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3">常用总结与问题解决</a><ul>
<li><a href="#%E5%B8%B8%E7%94%A8%E6%80%BB%E7%BB%93">常用总结</a></li>
<li><a href="#%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3">问题解决</a></li>
</ul>
</li>
<li><a href="#%E6%9C%8D%E5%8A%A1%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E8%B7%AF%E7%BA%BF">服务框架学习路线</a></li>
<li><a href="#springcloud%E5%AE%9E%E7%94%A8%E7%AF%87%E4%B8%80">springcloud实用篇一</a><ul>
<li><a href="#%E4%B8%80%E8%AE%A4%E8%AF%86%E5%BE%AE%E6%9C%8D%E5%8A%A1">一、认识微服务</a><ul>
<li><a href="#%E6%9C%8D%E5%8A%A1%E6%A1%86%E6%9E%B6%E6%BC%94%E5%8F%98">服务框架演变</a></li>
<li><a href="#%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%8A%80%E6%9C%AF%E5%AF%B9%E6%AF%94">微服务技术对比</a></li>
<li><a href="#springcloud">SpringCloud</a></li>
</ul>
</li>
<li><a href="#%E4%BA%8C%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%8B%86%E5%88%86%E6%A1%88%E4%BE%8B">二、微服务拆分案例</a><ul>
<li><a href="#%E6%9C%8D%E5%8A%A1%E6%8B%86%E5%88%86">服务拆分</a></li>
<li><a href="#%E6%A1%88%E4%BE%8Bcloud-demo">案例cloud-demo</a></li>
<li><a href="#%E6%A6%82%E5%BF%B5%E6%8F%90%E4%BE%9B%E8%80%85%E4%B8%8E%E6%B6%88%E8%B4%B9%E8%80%85">概念：提供者与消费者</a></li>
</ul>
</li>
<li><a href="#%E4%B8%89eureka%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83">三、eureka注册中心</a><ul>
<li><a href="#1%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8%E7%9A%84%E9%97%AE%E9%A2%98%E4%B8%80%E5%A0%86%E9%97%AE%E9%A2%98">1、远程调用的问题（一堆问题）</a></li>
<li><a href="#2eureka%E5%8E%9F%E7%90%86">2、eureka原理</a></li>
<li><a href="#3%E6%90%AD%E5%BB%BAeurekaserver%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83">3、搭建EurekaServer注册中心</a></li>
<li><a href="#4%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C">4、服务注册</a></li>
<li><a href="#5%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0">5、服务发现</a></li>
</ul>
</li>
<li><a href="#%E5%9B%9Bribbon%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%8E%9F%E7%90%86">四、Ribbon负载均衡原理</a><ul>
<li><a href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%8E%9F%E7%90%86">负载均衡原理</a></li>
<li><a href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%AD%96%E7%95%A5">负载均衡策略</a></li>
<li><a href="#%E6%87%92%E5%8A%A0%E8%BD%BD">懒加载</a></li>
</ul>
</li>
<li><a href="#%E4%BA%94nacos%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83">五、nacos注册中心</a><ul>
<li><a href="#1%E8%AE%A4%E8%AF%86%E5%92%8C%E5%AE%89%E8%A3%85">1、认识和安装</a></li>
<li><a href="#2%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8">2、快速入门</a></li>
<li><a href="#3%E6%9C%8D%E5%8A%A1%E5%88%86%E7%BA%A7%E5%AD%98%E5%82%A8%E6%A8%A1%E5%9E%8B">3、服务分级存储模型</a></li>
<li><a href="#4%E7%8E%AF%E5%A2%83%E9%9A%94%E7%A6%BBnamespace">4、环境隔离namespace</a><ul>
<li><a href="#%E5%A6%82%E4%BD%95%E4%BF%AE%E6%94%B9%E4%B8%80%E4%B8%AA%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4">如何修改一个服务的命名空间：</a></li>
<li><a href="#%E6%80%BB%E7%BB%93">总结</a></li>
<li><a href="#%E4%B8%B4%E6%97%B6%E5%AE%9E%E4%BE%8B%E5%92%8C%E9%9D%9E%E4%B8%B4%E6%97%B6%E5%AE%9E%E4%BE%8B">临时实例和非临时实例</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#springcloud%E5%AE%9E%E7%94%A8%E7%AF%87%E4%BA%8C">springcloud实用篇二</a><ul>
<li><a href="#%E4%B8%80nacos%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86">一、Nacos配置管理</a><ul>
<li><a href="#1%E7%BB%9F%E4%B8%80%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86">1、统一配置管理</a><ul>
<li><a href="#nacos%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86">Nacos实现配置管理</a></li>
<li><a href="#%E5%BE%AE%E6%9C%8D%E5%8A%A1%E9%85%8D%E7%BD%AE%E6%8B%89%E5%8F%96">微服务配置拉取</a></li>
</ul>
</li>
<li><a href="#2%E9%85%8D%E7%BD%AE%E7%83%AD%E6%9B%B4%E6%96%B0">2、配置热更新</a></li>
<li><a href="#3%E9%85%8D%E7%BD%AE%E5%85%B1%E4%BA%AB">3、配置共享</a></li>
<li><a href="#4%E6%90%AD%E5%BB%BAnacos%E9%9B%86%E7%BE%A4">4、搭建Nacos集群</a><ul>
<li><a href="#%E6%90%AD%E5%BB%BA%E9%9B%86%E7%BE%A4%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%AD%A5%E9%AA%A4">搭建集群的基本步骤：</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#%E4%BA%8Cfeign%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8http%E5%AE%A2%E6%88%B7%E7%AB%AFfeign">二、Feign远程调用（HTTP客户端Feign）</a><ul>
<li><a href="#1feign%E6%9B%BF%E4%BB%A3resttemplate">1、Feign替代RestTemplate</a><ul>
<li><a href="#11-resttemplate%E6%96%B9%E5%BC%8F%E8%B0%83%E7%94%A8%E5%AD%98%E5%9C%A8%E7%9A%84%E9%97%AE%E9%A2%98">1.1 RestTemplate方式调用存在的问题</a></li>
<li><a href="#12-feign%E7%9A%84%E4%BB%8B%E7%BB%8D">1.2 Feign的介绍</a></li>
<li><a href="#13-%E5%AE%9A%E4%B9%89%E5%92%8C%E4%BD%BF%E7%94%A8feign%E5%AE%A2%E6%88%B7%E7%AB%AF">1.3 定义和使用Feign客户端</a></li>
</ul>
</li>
<li><a href="#2%E8%87%AA%E5%AE%9A%E4%B9%89%E9%85%8D%E7%BD%AE">2、自定义配置</a><ul>
<li><a href="#%E9%85%8D%E7%BD%AEfeign%E6%97%A5%E5%BF%97%E6%9C%89%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F">配置Feign日志有两种方式：</a></li>
</ul>
</li>
<li><a href="#3figen%E4%BD%BF%E7%94%A8%E4%BC%98%E5%8C%96%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98">3、Figen使用优化（性能调优）</a><ul>
<li><a href="#%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%85%8D%E7%BD%AE">连接池配置</a></li>
</ul>
</li>
<li><a href="#4%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5">4、最佳实践</a><ul>
<li><a href="#feign%E7%9A%84%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5">Feign的最佳实践</a></li>
<li><a href="#%E5%AE%9E%E7%8E%B0feign%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5">实现Feign最佳实践</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#%E4%B8%89gateway%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3">三、Gateway服务网关</a><ul>
<li><a href="#1%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%E7%BD%91%E5%85%B3">1、为什么需要网关</a></li>
<li><a href="#2gateway%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8">2、gateway快速入门</a><ul>
<li><a href="#%E6%90%AD%E5%BB%BA%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3">搭建服务网关</a></li>
</ul>
</li>
<li><a href="#3%E6%96%AD%E8%A8%80%E5%B7%A5%E5%8E%82">3、断言工厂</a></li>
<li><a href="#4%E5%85%A8%E5%B1%80%E8%BF%87%E6%BB%A4%E5%99%A8">4、全局过滤器</a><ul>
<li><a href="#%E8%B7%AF%E7%94%B1%E8%BF%87%E6%BB%A4%E5%99%A8-gatewayfilter">路由过滤器 GatewayFilter</a></li>
<li><a href="#%E8%BF%87%E6%BB%A4%E5%99%A8%E5%B7%A5%E5%8E%82-gatewayfilterfactory">过滤器工厂 GatewayFilterFactory</a></li>
<li><a href="#%E6%A1%88%E4%BE%8B%E7%BB%99%E6%89%80%E6%9C%89%E8%BF%9B%E5%85%A5userservice%E7%9A%84%E8%AF%B7%E6%B1%82%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E8%AF%B7%E6%B1%82%E5%A4%B4truthitcast-is-freaking-awesome">案例：给所有进入userservice的请求添加一个请求头：Truth=itcast is freaking awesome!</a></li>
<li><a href="#%E5%85%A8%E5%B1%80%E8%BF%87%E6%BB%A4%E5%99%A8-globalfilter">全局过滤器 GlobalFilter</a></li>
<li><a href="#%E6%A1%88%E4%BE%8B%E5%AE%9A%E4%B9%89%E5%85%A8%E5%B1%80%E8%BF%87%E6%BB%A4%E5%99%A8%E6%8B%A6%E6%88%AA%E5%B9%B6%E5%88%A4%E6%96%AD%E7%94%A8%E6%88%B7%E8%BA%AB%E4%BB%BD">案例：定义全局过滤器，拦截并判断用户身份</a></li>
<li><a href="#%E8%BF%87%E6%BB%A4%E5%99%A8%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F">过滤器执行顺序</a></li>
</ul>
</li>
<li><a href="#5%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86">5、跨域问题处理</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h1 id="常用总结与问题解决"><a href="#常用总结与问题解决" class="headerlink" title="常用总结与问题解决"></a>常用总结与问题解决</h1><h2 id="常用总结"><a href="#常用总结" class="headerlink" title="常用总结"></a>常用总结</h2><blockquote>
<p>项目布局<br>cloud_demo – 父工程：定义版本    　　<br>　　├── eureka-server – 注册中心<br>　　├── feign-api – 远程调用接口定义<br>　　├── gateway – 网关配置<br>　　├── order-service – 订单服务<br>　　├── user-service – 用户服务<br>　　└── pom.xml – 父pom　</p>
</blockquote>
<blockquote>
<p>注册中心eureka：</p>
</blockquote>
<p>服务消费者该如何获取服务提供者的地址信息？</p>
<p>如果有多个服务提供者，消费者该如何选择？</p>
<p>消费者如何得知服务提供者的健康状态？</p>
<blockquote>
<p>负载均衡：</p>
</blockquote>
<blockquote>
<p>配置中心：</p>
</blockquote>
<blockquote>
<p>feign远程调用：  </p>
</blockquote>
<p>基于RestTemplate发起的http请求实现远程调用，只要知道对方的ip、端口、接口路径、请求参数即可。<br>一个服务既可以是提供者，也可以是消费者。</p>
<blockquote>
<p>Gateway服务网关：</p>
</blockquote>
<h2 id="问题解决"><a href="#问题解决" class="headerlink" title="问题解决"></a>问题解决</h2><h1 id="服务框架学习路线"><a href="#服务框架学习路线" class="headerlink" title="服务框架学习路线"></a>服务框架学习路线</h1><p><strong>完整微服务技术栈：微服务技术 + 持续集成</strong></p>
<blockquote>
<p>微服务并不是等于springcloud技术</p>
<p>消息队列：异步通信大大提高服务的并发：秒杀等高并发场景利用</p>
<p>系统监控链路追踪：实时监控每一个服务节点的运行状态、CPU负载、内存的占用等等情况，一旦出现任何的问题，可以直接定位到某一具体的方法栈信息，快速定位异常所在。</p>
<p>自动化部署：工具Jenkins，Docker打包形成镜像</p>
</blockquote>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211113438830.png"></p>
<p><strong>需要学习的微服务知识</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/%E5%BE%AE%E6%9C%8D%E5%8A%A11.png"></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211120632882.png"></p>
<p><strong>学习路径</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211120655865.png"></p>
<p><strong>分层次教学</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211122543190.png"></p>
<p><strong>知识层次</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/%E5%AD%A6%E4%B9%A0%E5%B1%82%E6%AC%A1.png"></p>
<h1 id="springcloud实用篇一"><a href="#springcloud实用篇一" class="headerlink" title="springcloud实用篇一"></a>springcloud实用篇一</h1><blockquote>
<p>主要内容：注册中心、负载均衡器</p>
</blockquote>
<h2 id="一、认识微服务"><a href="#一、认识微服务" class="headerlink" title="一、认识微服务"></a>一、认识微服务</h2><h3 id="服务框架演变"><a href="#服务框架演变" class="headerlink" title="服务框架演变"></a>服务框架演变</h3><p><strong>1、单体架构：将业务的所有功能集中在一个项目中开发，打成一个包部署。</strong><br><strong>2、分布式架构：根据业务功能对系统进行拆分，每个业务模块作为独立项目开发，称为一个服务。</strong><br><strong>3、微服务是一种经过良好架构设计的分布式架构方案，微服务架构特征：</strong></p>
<blockquote>
<p>单一职责：微服务拆分粒度更小，每一个服务都对应唯一的业务能力，做到单一职责，避免重复业务开发<br>面向服务：微服务对外暴露业务接口<br>自治：团队独立、技术独立、数据独立、部署独立<br>隔离性强：服务调用做好隔离、容错、降级，避免出现级联问题</p>
</blockquote>
<h3 id="微服务技术对比"><a href="#微服务技术对比" class="headerlink" title="微服务技术对比"></a>微服务技术对比</h3><p>微服务这种方案需要技术框架来落地，全球的互联网公司都在积极尝试自己的微服务落地技术。在国内最知名的就是SpringCloud和阿里巴巴的Dubbo。</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211125555526.png"></p>
<h3 id="SpringCloud"><a href="#SpringCloud" class="headerlink" title="SpringCloud"></a>SpringCloud</h3><p>SpringCloud是目前国内使用最广泛的微服务框架。官网地址：<a target="_blank" rel="noopener" href="https://spring.io/projects/spring-cloud">https://spring.io/projects/spring-cloud</a><br>SpringCloud集成了各种微服务功能组件，并基于SpringBoot实现了这些<strong>组件的自动装配</strong>，从而提供了良好的开箱即用体验：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211130417169.png"></p>
<p><strong>SpringCloud与SpringBoot的版本兼容关系如下：</strong></p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211130745877.png" alt="" style="zoom: 33%;" />





<h2 id="二、微服务拆分案例"><a href="#二、微服务拆分案例" class="headerlink" title="二、微服务拆分案例"></a>二、微服务拆分案例</h2><h3 id="服务拆分"><a href="#服务拆分" class="headerlink" title="服务拆分"></a>服务拆分</h3><p>父工程：用于定义版本</p>
<blockquote>
<p><strong>注意事项：</strong></p>
<p>单一职责：不同微服务，不要重复开发相同业务<br>数据独立：不要访问其它微服务的数据库<br>面向服务：将自己的业务暴露为接口，供其它微服务调用</p>
</blockquote>
<h3 id="案例cloud-demo"><a href="#案例cloud-demo" class="headerlink" title="案例cloud-demo"></a>案例cloud-demo</h3><p><strong>微服务远程调用–查询订单</strong></p>
<p><strong>1）注册RestTemplate</strong></p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span><span class="token string">"cn.itcast.order.mapper"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderApplication</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        SpringApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>OrderApplication<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/**
     * 创建RestTemplate并注入Spring容器
     */</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token annotation punctuation">@LoadBalanced</span>
    <span class="token keyword">public</span> RestTemplate <span class="token function">restTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">RestTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>2）服务远程调用RestTemplate（url中使用硬编码的方式）</strong></p>
<p>修改<code>order-service</code>服务中的<code>OrderService</code>类的<code>queryOrderById</code>方法：</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderService</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> RestTemplate restTemplate<span class="token punctuation">;</span>

    <span class="token keyword">public</span> Order <span class="token function">queryOrderById</span><span class="token punctuation">(</span>Long orderId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// 1.查询订单</span>
        Order order <span class="token operator">=</span> orderMapper<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span>orderId<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 2.利用RestTemplate发起http请求，查询用户</span>
        <span class="token comment" spellcheck="true">// 2.1.url路径</span>
        String url <span class="token operator">=</span> <span class="token string">"http://localhost:8081/user/"</span> <span class="token operator">+</span> order<span class="token punctuation">.</span><span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 2.2.发送http请求，实现远程调用</span>
        User user <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> User<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 3.封装user到Order</span>
        order<span class="token punctuation">.</span><span class="token function">setUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 4.返回</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211162822962.png"></p>
<p>基于RestTemplate发起的http请求实现远程调用<br>http请求做远程调用是与语言无关的调用，只要知道对方的ip、端口、接口路径、请求参数即可。</p>
<h3 id="概念：提供者与消费者"><a href="#概念：提供者与消费者" class="headerlink" title="概念：提供者与消费者"></a>概念：提供者与消费者</h3><p>服务提供者：一次业务中，被其它微服务调用的服务。（提供接口给其它微服务）<br>服务消费者：一次业务中，调用其它微服务的服务。（调用其它微服务提供的接口）</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211164700331.png"></p>
<p>服务A调用服务B，服务B调用服务C，那么服务B是什么角色？</p>
<p>事实上，一个服务既可以是提供者，也可以是消费者。</p>
<h2 id="三、eureka注册中心"><a href="#三、eureka注册中心" class="headerlink" title="三、eureka注册中心"></a>三、eureka注册中心</h2><h3 id="1、远程调用的问题（一堆问题）"><a href="#1、远程调用的问题（一堆问题）" class="headerlink" title="1、远程调用的问题（一堆问题）"></a>1、远程调用的问题（一堆问题）</h3><p>服务消费者该如何获取服务提供者的地址信息？</p>
<blockquote>
<p>服务提供者启动时向eureka注册自己的信息<br>eureka保存这些信息<br>消费者根据服务名称向eureka拉取提供者信息</p>
</blockquote>
<p>如果有多个服务提供者，消费者该如何选择？</p>
<blockquote>
<p>服务消费者利用负载均衡算法，从服务列表中挑选一个</p>
</blockquote>
<p>消费者如何得知服务提供者的健康状态？</p>
<blockquote>
<p>服务提供者会每隔30秒向EurekaServer发送心跳请求，报告健康状态<br>eureka会更新记录服务列表信息，心跳不正常会被剔除<br>消费者就可以拉取到最新的信息</p>
</blockquote>
<h3 id="2、eureka原理"><a href="#2、eureka原理" class="headerlink" title="2、eureka原理"></a>2、eureka原理</h3><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211165752560.png" alt="" style="zoom: 40%;" />



<h3 id="3、搭建EurekaServer注册中心"><a href="#3、搭建EurekaServer注册中心" class="headerlink" title="3、搭建EurekaServer注册中心"></a>3、搭建EurekaServer注册中心</h3><p>创建项目，引入spring-cloud-starter-netflix-eureka-server的依赖</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--eureka服务端--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-eureka-server<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>编写启动类，添加<code>@EnableEurekaServer</code>注解</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@EnableEurekaServer</span>
</code></pre>
<p>添加application.yml文件，编写下面的配置：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">10086 </span><span class="token comment" spellcheck="true"># 服务端口</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> eurekaserver <span class="token comment" spellcheck="true"># eureka的服务名称</span>
<span class="token key atrule">eureka</span><span class="token punctuation">:</span>
  <span class="token key atrule">client</span><span class="token punctuation">:</span>
    <span class="token key atrule">service-url</span><span class="token punctuation">:</span>  <span class="token comment" spellcheck="true"># eureka的地址信息</span>
      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//127.0.0.1<span class="token punctuation">:</span>10086/eureka
</code></pre>
<h3 id="4、服务注册"><a href="#4、服务注册" class="headerlink" title="4、服务注册"></a>4、服务注册</h3><p>引入<code>spring-cloud-starter-netflix-eureka-client</code>的依赖</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--eureka客户端依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-eureka-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>在application.yml文件，编写下面的配置：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> userservice
<span class="token key atrule">eureka</span><span class="token punctuation">:</span>
  <span class="token key atrule">client</span><span class="token punctuation">:</span>
    <span class="token key atrule">service-url</span><span class="token punctuation">:</span>  <span class="token comment" spellcheck="true"># eureka的地址信息</span>
      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//127.0.0.1<span class="token punctuation">:</span>10086/eureka
</code></pre>
<p>另外，我们可以将<code>user-service</code>多次启动， 模拟多实例部署，但为了避免端口冲突，需要修改端口设置：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211172410982.png"></p>
<h3 id="5、服务发现"><a href="#5、服务发现" class="headerlink" title="5、服务发现"></a>5、服务发现</h3><p>在order-service完成服务拉取</p>
<p>服务拉取是基于服务名称获取服务列表，然后再对服务列表做负载均衡</p>
<p>修改OrderService的代码，修改访问的<code>url</code>路径，用<code>服务名</code>代替<code>ip</code>、<code>端口</code>：</p>
<pre class=" language-java"><code class="language-java">String url <span class="token operator">=</span> <span class="token string">"http://userservice/user/"</span> <span class="token operator">+</span> order<span class="token punctuation">.</span><span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>在<code>order-service</code>项目的启动类<code>OrderApplication</code>中的<code>RestTemplate</code>添加负载均衡注解：<code>@LoadBalanced</code></p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Bean</span>
<span class="token annotation punctuation">@LoadBalanced</span>
<span class="token keyword">public</span> RestTemplate <span class="token function">restTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">RestTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="四、Ribbon负载均衡原理"><a href="#四、Ribbon负载均衡原理" class="headerlink" title="四、Ribbon负载均衡原理"></a>四、Ribbon负载均衡原理</h2><h3 id="负载均衡原理"><a href="#负载均衡原理" class="headerlink" title="负载均衡原理"></a>负载均衡原理</h3><p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211174139422.png" alt="image-20211211174139422"></p>
<h3 id="负载均衡策略"><a href="#负载均衡策略" class="headerlink" title="负载均衡策略"></a>负载均衡策略</h3><p>Ribbon的负载均衡规则是一个叫做<code>IRule</code>的接口来定义的，每一个子接口都是一种规则：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211174332772.png"></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211174904487.png"></p>
<p><strong>通过定义IRule实现可以修改负载均衡规则，有两种方式：</strong></p>
<p>（1）代码方式：在order-service中的<code>OrderApplication</code>类中，定义一个新的<code>IRule</code>：（全局配置）</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> IRule <span class="token function">randomRule</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">RandomRule</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>（2）配置文件方式：在order-service的<code>application.yml</code>文件中，添加新的配置也可以修改规则：（针对某个微服务而言）</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">userservice</span><span class="token punctuation">:</span>
  <span class="token key atrule">ribbon</span><span class="token punctuation">:</span>
    <span class="token key atrule">NFLoadBalancerRuleClassName</span><span class="token punctuation">:</span> com.netflix.loadbalancer.RandomRule<span class="token comment" spellcheck="true"># 负载均衡规则 </span>
</code></pre>
<h3 id="懒加载"><a href="#懒加载" class="headerlink" title="懒加载"></a>懒加载</h3><p><code>Ribbon</code>默认是采用懒加载，即第一次访问时才会去创建<code>LoadBalanceClient</code>，请求时间会很长。<br>而饥饿加载则会在项目启动时创建，降低第一次访问的耗时，通过下面配置开启饥饿加载：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">ribbon</span><span class="token punctuation">:</span>
  <span class="token key atrule">eager-load</span><span class="token punctuation">:</span>
    <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true </span><span class="token comment" spellcheck="true"># 开启饥饿加载</span>
    <span class="token key atrule">clients</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 指定饥饿加载的服务名称</span>
      <span class="token punctuation">-</span> userservice
</code></pre>
<blockquote>
<p>总结一下：</p>
<p>（1）Ribbon负载均衡规则<br>        规则接口是IRule<br>        默认实现是ZoneAvoidanceRule，根据zone选择服务列表，然后轮询<br>（2）负载均衡自定义方式<br>        代码方式：配置灵活，但修改时需要重新打包发布<br>        配置方式：直观，方便，无需重新打包发布，但是无法做全局配置<br>（3）饥饿加载<br>        开启饥饿加载<br>        指定饥饿加载的微服务名称</p>
</blockquote>
<h2 id="五、nacos注册中心"><a href="#五、nacos注册中心" class="headerlink" title="五、nacos注册中心"></a>五、nacos注册中心</h2><h3 id="1、认识和安装"><a href="#1、认识和安装" class="headerlink" title="1、认识和安装"></a>1、认识和安装</h3><p>Nacos是阿里巴巴的产品，现在是SpringCloud中的一个组件。相比Eureka功能更加丰富，在国内受欢迎程度较高。</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211182315838.png"></p>
<h3 id="2、快速入门"><a href="#2、快速入门" class="headerlink" title="2、快速入门"></a>2、快速入门</h3><p>在<code>cloud-demo</code>父工程中添加<code>spring-cloud-alilbaba</code>的管理依赖：</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--nacos的管理依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-alibaba-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.2.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>注释掉<code>order-service</code>和<code>user-service</code>中原有的<code>eureka</code>依赖。<br>添加<code>nacos</code>的客户端依赖：</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!-- nacos客户端依赖包 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>修改<code>user-service</code>&amp;<code>order-service</code>中的<code>application.yml</code>文件，注释eureka地址，添加nacos地址：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848 </span><span class="token comment" spellcheck="true"># nacos服务地址</span>
</code></pre>
<p>启动并测试：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211191456264.png"></p>
<h3 id="3、服务分级存储模型"><a href="#3、服务分级存储模型" class="headerlink" title="3、服务分级存储模型"></a>3、服务分级存储模型</h3><p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211194746398.png"></p>
<p><strong>3.1 服务跨集群调用问题</strong></p>
<blockquote>
<p>服务调用尽可能选择本地集群的服务，跨集群调用延迟较高<br>本地集群不可访问时，再去访问其它集群</p>
</blockquote>
<p><strong>3.2 服务集群属性</strong></p>
<p>修改application.yml，添加如下内容：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211231019257.png"></p>
<p>在Nacos控制台可以看到集群变化：（我们修改user-service集群属性配置，达到下面的效果：）</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211231239040.png"></p>
<p><strong>3.3 总结：</strong></p>
<p>Nacos服务分级存储模型：</p>
<ul>
<li>一级是<font color=red>服务</font>，例如userservice</li>
<li>二级是<font color=red>集群</font>，例如杭州或上海</li>
<li>三级是<font color=red>实例</font>，例如杭州机房的某台部署了userservice的服务器</li>
</ul>
<p>如何设置实例的集群属性：<br>        修改application.yml文件，添加<code>spring.cloud.nacos.discovery.cluster-name</code>属性即可</p>
<p><strong>3.4 根据集群负载均衡</strong></p>
<blockquote>
<p>我们希望优先选择HZ集群（8001和8002）</p>
<p>进行如下设置：</p>
</blockquote>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211231610325.png"></p>
<blockquote>
<p>1）修改order-service中的application.yml，设置集群为HZ：</p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211231905497.png" alt="" style="zoom:33%;" />

<p>2）然后在order-service中设置负载均衡的IRule为<strong>NacosRule</strong>，这个规则<strong>优先会寻找与自己同集群的服务</strong>：</p>
<p>NacosRule负载均衡策略：先本地，然后本地内随机</p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211231930721.png" alt="" style="zoom: 33%;" />
</blockquote>
<p>跨集群访问的警告信息：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211232440450.png"></p>
<p> <strong>3.5 根据权重负载均衡</strong></p>
<p>实际部署中会出现的场景：服务器设备性能有差异，部分实例所在机器性能较好，另一些较差，我们<strong>希望性能好的机器承担更多的用户请求</strong></p>
<p>Nacos提供了<strong>权重配置来控制访问频率，权重越大则访问频率越高</strong></p>
<p><strong>实践：</strong></p>
<p>在Nacos控制台可以设置实例的权重值，首先选中实例后面的编辑按钮</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211232908311.png"></p>
<p>将权重设置为0.1，测试可以发现8081被访问到的频率大大降低</p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211232935847.png" alt="" style="zoom: 33%;" />



<blockquote>
<p>实例的权重控制：<br>        Nacos控制台可以设置实例的权重值，0~1之间<br>        同集群内的多个实例，权重越高被访问的频率越高<br>        权重设置为<font color=red>0</font>则完全不会被访问</p>
</blockquote>
<h3 id="4、环境隔离namespace"><a href="#4、环境隔离namespace" class="headerlink" title="4、环境隔离namespace"></a>4、环境隔离namespace</h3><h4 id="如何修改一个服务的命名空间："><a href="#如何修改一个服务的命名空间：" class="headerlink" title="如何修改一个服务的命名空间："></a>如何修改一个服务的命名空间：</h4><p><strong>1）在Nacos控制台可以创建namespace，用来隔离不同环境</strong></p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211233943719.png" alt="" style="zoom:33%;" />

<p><strong>2）然后填写一个新的命名空间信息：</strong></p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211234013400.png" alt="" style="zoom:33%;" />

<p><strong>3）保存后会在控制台看到这个命名空间的<code>id</code>：</strong></p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211234038201.png" alt="" style="zoom:33%;" />

<p><strong>4）修改order-service的application.yml，添加namespace：</strong></p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">datasource</span><span class="token punctuation">:</span>
    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span>3306/cloud_order<span class="token punctuation">?</span>useSSL=false
    <span class="token key atrule">username</span><span class="token punctuation">:</span> root
    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token number">1</span>
    <span class="token key atrule">driver-class-name</span><span class="token punctuation">:</span> com.mysql.jdbc.Driver
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> orderservice
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848 </span><span class="token comment" spellcheck="true"># nacos服务地址</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">cluster-name</span><span class="token punctuation">:</span> SH <span class="token comment" spellcheck="true"># 上海</span>
        <span class="token key atrule">namespace</span><span class="token punctuation">:</span> 4d6ce343<span class="token punctuation">-</span>9e1b<span class="token punctuation">-</span>44df<span class="token punctuation">-</span>a90f<span class="token punctuation">-</span>2cf2b6b3d177 <span class="token comment" spellcheck="true"># dev环境   命名空间，填ID</span>
</code></pre>
<p><strong>5）重启order-service后，再来查看控制台：</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211234615252.png"></p>
<p><strong>6）此时访问order-service，因为namespace不同，会导致找不到userservice，控制台会报错：</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211234632036.png"></p>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><p>Nacos环境隔离<br>1）每个namespace都有唯一id<br>2）服务设置namespace时要写id而不是名称<br>3）不同namespace下的服务互相不可见</p>
<h4 id="临时实例和非临时实例"><a href="#临时实例和非临时实例" class="headerlink" title="临时实例和非临时实例"></a>临时实例和非临时实例</h4><p><strong>好好理解</strong></p>
<blockquote>
<p>服务注册到Nacos时，可以选择注册为临时或非临时实例，通过下面的配置来设置：</p>
<p>临时实例宕机时，会从nacos的服务列表中剔除，而非临时实例则不会</p>
</blockquote>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">ephemeral</span><span class="token punctuation">:</span> <span class="token boolean important">false </span><span class="token comment" spellcheck="true"># 是否是临时实例</span>
</code></pre>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211211235219524.png"></p>
<p><strong>Nacos与eureka的共同点</strong></p>
<blockquote>
<p>都支持服务注册和服务拉取<br>都支持服务提供者心跳方式做健康检测</p>
</blockquote>
<p><strong>Nacos与Eureka的区别</strong></p>
<blockquote>
<p>Nacos支持服务端主动检测提供者状态：临时实例采用心跳模式，非临时实例采用主动检测模式<br>临时实例心跳不正常会被剔除，非临时实例则不会被剔除<br>Nacos支持服务列表变更的消息推送模式，服务列表更新更及时<br>Nacos集群默认采用AP方式，当集群中存在非临时实例时，采用CP模式；Eureka采用AP方式</p>
</blockquote>
<h1 id="springcloud实用篇二"><a href="#springcloud实用篇二" class="headerlink" title="springcloud实用篇二"></a>springcloud实用篇二</h1><h2 id="一、Nacos配置管理"><a href="#一、Nacos配置管理" class="headerlink" title="一、Nacos配置管理"></a>一、Nacos配置管理</h2><h3 id="1、统一配置管理"><a href="#1、统一配置管理" class="headerlink" title="1、统一配置管理"></a>1、统一配置管理</h3><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212093955308.png" alt="" style="zoom:33%;" />

<h4 id="Nacos实现配置管理"><a href="#Nacos实现配置管理" class="headerlink" title="Nacos实现配置管理"></a>Nacos实现配置管理</h4><p>在Nacos中添加配置信息：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212094350600.png"></p>
<p>在弹出表单中填写配置信息：</p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212094447628.png" alt="" style="zoom:33%;" />

<h4 id="微服务配置拉取"><a href="#微服务配置拉取" class="headerlink" title="微服务配置拉取"></a>微服务配置拉取</h4><p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212094829855.png"></p>
<p>（1）引入Nacos的配置管理客户端依赖：</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--nacos的配置管理依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-config<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>（2）在userservice中的<code>resource</code>目录添加一个<code>bootstrap.yml</code>文件，这个文件是引导文件，优先级高于<code>application.yml</code>：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> userservice
  <span class="token key atrule">profiles</span><span class="token punctuation">:</span>
    <span class="token key atrule">active</span><span class="token punctuation">:</span> dev <span class="token comment" spellcheck="true"># 环境</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848 </span><span class="token comment" spellcheck="true"># nacos地址</span>
      <span class="token key atrule">config</span><span class="token punctuation">:</span>
        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yaml <span class="token comment" spellcheck="true"># 文件后缀名</span>
</code></pre>
<p>（3）我们在user-service中将<code>pattern.dateformat</code>这个属性注入到UserController中做测试：</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/user"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token punctuation">{</span>
        
      <span class="token comment" spellcheck="true">// 注入nacos中的配置属性</span>
    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${pattern.dateformat}"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> String dateformat<span class="token punctuation">;</span>
        
      <span class="token comment" spellcheck="true">// 编写controller，通过日期格式化器来格式化现在时间并返回</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"now"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> String <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> LocalDateTime<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span>properties<span class="token punctuation">.</span><span class="token function">getDateformat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>总结将配置交给Nacos管理的步骤</strong></p>
<blockquote>
<p>在Nacos中添加配置文件<br>在微服务中引入nacos的config依赖<br>在微服务中添加bootstrap.yml，配置nacos地址、当前环境、服务名称、文件后缀名。这些决定了程序启动时去nacos读取哪个文件</p>
</blockquote>
<h3 id="2、通过配置实现微服务热更新"><a href="#2、通过配置实现微服务热更新" class="headerlink" title="2、通过配置实现微服务热更新"></a>2、通过配置实现微服务热更新</h3><p>Nacos中的配置文件变更后，微服务无需重启就可以感知。不过需要通过下面两种配置实现：</p>
<p>方式一：在@Value注入的变量所在类上添加注解<code>@RefreshScope</code></p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Slf4j</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/user"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@RefreshScope</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${pattern.dateformat}"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> String dateformat<span class="token punctuation">;</span>
</code></pre>
<p>方式二：使用<code>@ConfigurationProperties</code>注解（推荐）</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@Component</span>
<span class="token annotation punctuation">@ConfigurationProperties</span><span class="token punctuation">(</span>prefix <span class="token operator">=</span> <span class="token string">"pattern"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PatternProperties</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> String dateformat<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String envSharedValue<span class="token punctuation">;</span>
    <span class="token keyword">private</span> String name<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>注意事项：<br>    不是所有的配置都适合放到配置中心，维护起来比较麻烦<br>    建议将一些关键参数，需要运行时调整的参数放到nacos配置中心，一般都是自定义配置</p>
</blockquote>
<h3 id="3、配置共享"><a href="#3、配置共享" class="headerlink" title="3、配置共享"></a>3、配置共享</h3><h4 id="微服务启动时会从nacos读取多个配置文件："><a href="#微服务启动时会从nacos读取多个配置文件：" class="headerlink" title="微服务启动时会从nacos读取多个配置文件："></a>微服务启动时会从nacos读取多个配置文件：</h4><p>​        <code>[spring.application.name]-[spring.profiles.active].yaml</code>，例如：userservice-dev.yaml<br>​        <code>[spring.application.name].yaml</code>，例如：userservice.yaml<br>无论profile如何变化，[spring.application.name].yaml这个文件一定会加载，因此多环境共享配置可以写入这个文件</p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212131127779.png" alt="" style="zoom:50%;" />



<h4 id="多种配置的优先级："><a href="#多种配置的优先级：" class="headerlink" title="多种配置的优先级："></a>多种配置的优先级：</h4><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212131225553.png" alt="" style="zoom:50%;" />

<h4 id="不同微服务之间可以共享配置文件，通过下面的两种方式来指定："><a href="#不同微服务之间可以共享配置文件，通过下面的两种方式来指定：" class="headerlink" title="不同微服务之间可以共享配置文件，通过下面的两种方式来指定："></a>不同微服务之间可以共享配置文件，通过下面的两种方式来指定：</h4><p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212131623849.png"></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212131641085.png"></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212131834352.png"></p>
<h4 id="总结-1"><a href="#总结-1" class="headerlink" title="总结"></a>总结</h4><p><strong>微服务默认读取的配置文件：</strong></p>
<blockquote>
<p>[服务名]-[spring.profile.active].yaml，默认配置<br>[服务名].yaml，多环境共享</p>
</blockquote>
<p><strong>不同微服务共享的配置文件：</strong></p>
<blockquote>
<p>通过shared-configs指定<br>通过extension-configs指定</p>
</blockquote>
<p><strong>优先级：</strong></p>
<blockquote>
<p>环境配置 &gt;服务名.yaml &gt; extension-config &gt; extension-configs &gt; shared-configs &gt; 本地配置</p>
</blockquote>
<h3 id="4、搭建Nacos集群"><a href="#4、搭建Nacos集群" class="headerlink" title="4、搭建Nacos集群"></a>4、搭建Nacos集群</h3><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211212132653512.png" alt="" style="zoom:33%;" />



<h4 id="搭建集群的基本步骤："><a href="#搭建集群的基本步骤：" class="headerlink" title="搭建集群的基本步骤："></a>搭建集群的基本步骤：</h4><ul>
<li>搭建数据库，初始化数据库表结构</li>
<li>下载nacos安装包</li>
<li>配置nacos</li>
<li>启动nacos集群</li>
<li>nginx反向代理</li>
</ul>
<p><strong>1）初始化数据库</strong></p>
<p>Nacos默认数据存储在内嵌数据库<code>Derby</code>中，不属于生产可用的数据库。</p>
<p>官方推荐的最佳实践是使用带有主从的高可用数据库集群，主从模式的高可用数据库可以参考<strong>传智教育</strong>的后续高手课程。</p>
<p>这里我们以单点的数据库为例来讲解。</p>
<p>首先新建一个数据库，命名为<code>nacos</code>，而后导入下面的SQL：</p>
<pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>config_info<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>data_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'data_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>content<span class="token punctuation">`</span> <span class="token keyword">longtext</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'content'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>md5<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'md5'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'创建时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_user<span class="token punctuation">`</span> <span class="token keyword">text</span> <span class="token keyword">COMMENT</span> <span class="token string">'source user'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_ip<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'source ip'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>app_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'租户字段'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>c_desc<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">256</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>c_use<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>effect<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span><span class="token keyword">type</span><span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>c_schema<span class="token punctuation">`</span> <span class="token keyword">text</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_configinfo_datagrouptenant<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>data_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>group_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'config_info'</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = config_info_aggr   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>config_info_aggr<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>data_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'data_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'group_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>datum_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'datum_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>content<span class="token punctuation">`</span> <span class="token keyword">longtext</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'内容'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>app_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'租户字段'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_configinfoaggr_datagrouptenantdatum<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>data_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>group_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>datum_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'增加租户字段'</span><span class="token punctuation">;</span>


<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = config_info_beta   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>config_info_beta<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>data_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'data_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'group_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>app_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'app_name'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>content<span class="token punctuation">`</span> <span class="token keyword">longtext</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'content'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>beta_ips<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'betaIps'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>md5<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'md5'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'创建时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_user<span class="token punctuation">`</span> <span class="token keyword">text</span> <span class="token keyword">COMMENT</span> <span class="token string">'source user'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_ip<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'source ip'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'租户字段'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_configinfobeta_datagrouptenant<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>data_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>group_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'config_info_beta'</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = config_info_tag   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>config_info_tag<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>data_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'data_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'group_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'tenant_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tag_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'tag_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>app_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'app_name'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>content<span class="token punctuation">`</span> <span class="token keyword">longtext</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'content'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>md5<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'md5'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'创建时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_user<span class="token punctuation">`</span> <span class="token keyword">text</span> <span class="token keyword">COMMENT</span> <span class="token string">'source user'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_ip<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'source ip'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_configinfotag_datagrouptenanttag<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>data_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>group_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tag_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'config_info_tag'</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = config_tags_relation   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>config_tags_relation<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tag_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'tag_name'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tag_type<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'tag_type'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>data_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'data_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'group_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'tenant_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>nid<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>nid<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_configtagrelation_configidtag<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tag_name<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tag_type<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">KEY</span> <span class="token punctuation">`</span>idx_tenant_id<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'config_tag_relation'</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = group_capacity   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>group_capacity<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'主键ID'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'Group ID，空字符表示整个集群'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>quota<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'配额，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span><span class="token keyword">usage</span><span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'使用量'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_size<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'单个配置大小上限，单位为字节，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_aggr_count<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'聚合子配置最大个数，，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_aggr_size<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'单个聚合数据的子配置大小上限，单位为字节，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_history_count<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'最大变更历史数量'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'创建时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_group_id<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>group_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'集群、各Group容量信息表'</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = his_config_info   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>his_config_info<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>nid<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>data_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>group_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>app_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'app_name'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>content<span class="token punctuation">`</span> <span class="token keyword">longtext</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>md5<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_user<span class="token punctuation">`</span> <span class="token keyword">text</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>src_ip<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>op_type<span class="token punctuation">`</span> char<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'租户字段'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>nid<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">KEY</span> <span class="token punctuation">`</span>idx_gmt_create<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">KEY</span> <span class="token punctuation">`</span>idx_gmt_modified<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">KEY</span> <span class="token punctuation">`</span>idx_did<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>data_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'多租户改造'</span><span class="token punctuation">;</span>


<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token comment" spellcheck="true">/*   数据库全名 = nacos_config   */</span>
<span class="token comment" spellcheck="true">/*   表名称 = tenant_capacity   */</span>
<span class="token comment" spellcheck="true">/******************************************/</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>tenant_capacity<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'主键ID'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'Tenant ID'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>quota<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'配额，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span><span class="token keyword">usage</span><span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'使用量'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_size<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'单个配置大小上限，单位为字节，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_aggr_count<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'聚合子配置最大个数'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_aggr_size<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'单个聚合数据的子配置大小上限，单位为字节，0表示使用默认值'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>max_history_count<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> unsigned <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">'0'</span> <span class="token keyword">COMMENT</span> <span class="token string">'最大变更历史数量'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'创建时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_tenant_id<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'租户容量信息表'</span><span class="token punctuation">;</span>


<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>tenant_info<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>kp<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'kp'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">default</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'tenant_id'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token keyword">default</span> <span class="token string">''</span> <span class="token keyword">COMMENT</span> <span class="token string">'tenant_name'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>tenant_desc<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">256</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'tenant_desc'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>create_source<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'create_source'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_create<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'创建时间'</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>gmt_modified<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'修改时间'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>uk_tenant_info_kptenantid<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>kp<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">KEY</span> <span class="token punctuation">`</span>idx_tenant_id<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>tenant_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8 <span class="token keyword">COLLATE</span><span class="token operator">=</span>utf8_bin <span class="token keyword">COMMENT</span><span class="token operator">=</span><span class="token string">'tenant_info'</span><span class="token punctuation">;</span>

<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>users<span class="token punctuation">`</span> <span class="token punctuation">(</span>
    <span class="token punctuation">`</span>username<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span><span class="token punctuation">,</span>
    <span class="token punctuation">`</span>password<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    <span class="token punctuation">`</span>enabled<span class="token punctuation">`</span> <span class="token keyword">boolean</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>roles<span class="token punctuation">`</span> <span class="token punctuation">(</span>
    <span class="token punctuation">`</span>username<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    <span class="token punctuation">`</span>role<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    <span class="token keyword">UNIQUE</span> <span class="token keyword">INDEX</span> <span class="token punctuation">`</span>idx_user_role<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>username<span class="token punctuation">`</span> <span class="token keyword">ASC</span><span class="token punctuation">,</span> <span class="token punctuation">`</span>role<span class="token punctuation">`</span> <span class="token keyword">ASC</span><span class="token punctuation">)</span> <span class="token keyword">USING</span> <span class="token keyword">BTREE</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>permissions<span class="token punctuation">`</span> <span class="token punctuation">(</span>
    <span class="token punctuation">`</span>role<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    <span class="token punctuation">`</span>resource<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    <span class="token punctuation">`</span><span class="token keyword">action</span><span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    <span class="token keyword">UNIQUE</span> <span class="token keyword">INDEX</span> <span class="token punctuation">`</span>uk_role_permission<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>role<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>resource<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span><span class="token keyword">action</span><span class="token punctuation">`</span><span class="token punctuation">)</span> <span class="token keyword">USING</span> <span class="token keyword">BTREE</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> users <span class="token punctuation">(</span>username<span class="token punctuation">,</span> password<span class="token punctuation">,</span> enabled<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'nacos'</span><span class="token punctuation">,</span> <span class="token string">'$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu'</span><span class="token punctuation">,</span> <span class="token boolean">TRUE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> roles <span class="token punctuation">(</span>username<span class="token punctuation">,</span> role<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'nacos'</span><span class="token punctuation">,</span> <span class="token string">'ROLE_ADMIN'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><strong>2）下载nacos</strong></p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/nacos/tags">https://github.com/alibaba/nacos/tags</a></p>
<p>可以选择任意版本下载。本例中采用<code>1.4.1</code>版本。</p>
<p><strong>3）配置nacos</strong></p>
<p>解压                     </p>
<blockquote>
<p>其中<code>bin</code>中是启动脚本  <code>conf</code>是配置文件</p>
</blockquote>
<p>进入nacos的conf目录，修改配置文件<code>cluster.conf.example</code>，重命名为<code>cluster.conf</code></p>
<p>然后添加内容：</p>
<pre class=" language-properties"><code class="language-properties"><span class="token attr-name">127.0.0.1</span><span class="token punctuation">:</span><span class="token attr-value">8845</span>
127.0.0.1.8846
127.0.0.1.8847
</code></pre>
<p>然后修改<code>application.properties</code>文件，添加数据库配置</p>
<pre class=" language-properties"><code class="language-properties"><span class="token attr-name">spring.datasource.platform</span><span class="token punctuation">=</span><span class="token attr-value">mysql</span>

<span class="token attr-name">db.num</span><span class="token punctuation">=</span><span class="token attr-value">1</span>

<span class="token attr-name">db.url.0</span><span class="token punctuation">=</span><span class="token attr-value">jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&amp;connectTimeout=1000&amp;socketTimeout=3000&amp;autoReconnect=true&amp;useUnicode=true&amp;useSSL=false&amp;serverTimezone=UTC</span>
<span class="token attr-name">db.user.0</span><span class="token punctuation">=</span><span class="token attr-value">root</span>
<span class="token attr-name">db.password.0</span><span class="token punctuation">=</span><span class="token attr-value">123</span>
</code></pre>
<p><strong>4）启动</strong></p>
<p>将nacos文件夹复制三份，分别命名为：<code>nacos1</code>、<code>nacos2</code>、<code>nacos3</code></p>
<p>然后分别修改三个文件夹中的application.properties，</p>
<p>nacos1:</p>
<pre class=" language-properties"><code class="language-properties"><span class="token attr-name">server.port</span><span class="token punctuation">=</span><span class="token attr-value">8845</span>
</code></pre>
<p>nacos2:</p>
<pre class=" language-properties"><code class="language-properties"><span class="token attr-name">server.port</span><span class="token punctuation">=</span><span class="token attr-value">8846</span>
</code></pre>
<p>nacos3:</p>
<pre class=" language-properties"><code class="language-properties"><span class="token attr-name">server.port</span><span class="token punctuation">=</span><span class="token attr-value">8847</span>
</code></pre>
<p>然后分别启动三个nacos节点：</p>
<pre class=" language-properties"><code class="language-properties">startup.cmd
</code></pre>
<p><strong>5）nginx反向代理</strong></p>
<p><code>nginx</code>安装包解压</p>
<p>修改<code>conf/nginx.conf</code>文件，配置如下：</p>
<pre class=" language-yaml"><code class="language-yaml">upstream nacos<span class="token punctuation">-</span>cluster <span class="token punctuation">{</span>
        server 127.0.0.1<span class="token punctuation">:</span>8845;
        server 127.0.0.1<span class="token punctuation">:</span>8846;
        server 127.0.0.1<span class="token punctuation">:</span>8847;
<span class="token punctuation">}</span>

server <span class="token punctuation">{</span>
    listen       80;
    server_name  localhost;

    location /nacos <span class="token punctuation">{</span>
        proxy_pass http<span class="token punctuation">:</span>//nacos<span class="token punctuation">-</span>cluster;
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="二、Feign远程调用（HTTP客户端Feign）"><a href="#二、Feign远程调用（HTTP客户端Feign）" class="headerlink" title="二、Feign远程调用（HTTP客户端Feign）"></a>二、Feign远程调用（HTTP客户端Feign）</h2><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">// vaule和name 其实是一个属性</span>
<span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">FeignClient</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@AliasFor</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span>
    String <span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">default</span> <span class="token string">""</span><span class="token punctuation">;</span>

    <span class="token annotation punctuation">@AliasFor</span><span class="token punctuation">(</span><span class="token string">"value"</span><span class="token punctuation">)</span>
    String <span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">default</span> <span class="token string">""</span><span class="token punctuation">;</span>
      <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@FeignClient</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"${feign.name.element2:element-server2}"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IBaseDataFeignClient</span> <span class="token keyword">extends</span> <span class="token class-name">IBaseDataFeignController</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="1、Feign替代RestTemplate"><a href="#1、Feign替代RestTemplate" class="headerlink" title="1、Feign替代RestTemplate"></a>1、Feign替代RestTemplate</h3><h4 id="1-1-RestTemplate方式调用存在的问题"><a href="#1-1-RestTemplate方式调用存在的问题" class="headerlink" title="1.1 RestTemplate方式调用存在的问题"></a>1.1 RestTemplate方式调用存在的问题</h4><pre class=" language-java"><code class="language-java">String url <span class="token operator">=</span> <span class="token string">"http://userservice/user/"</span> <span class="token operator">+</span> order<span class="token punctuation">.</span><span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
User user <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> User<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>代码可读性差，编程体验不统一；参数复杂URL难以维护</p>
<h4 id="1-2-Feign的介绍"><a href="#1-2-Feign的介绍" class="headerlink" title="1.2 Feign的介绍"></a>1.2 Feign的介绍</h4><p>官方地址：<a target="_blank" rel="noopener" href="https://github.com/OpenFeign/feign">https://github.com/OpenFeign/feign</a><br>Feign是一个声明式的<code>http</code>客户端，其作用就是帮助我们优雅的实现<code>http请求</code>的发送，解决上面提到的问题。</p>
<blockquote>
<p>理解声明式：如spring的声明式事务，对谁加事务规则定义好，spring来实现事务。</p>
<p>feign接口定义：发http请求的信息写出来就可以</p>
</blockquote>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213101049566.png" alt="" style="zoom:33%;" />



<h4 id="1-3-定义和使用Feign客户端"><a href="#1-3-定义和使用Feign客户端" class="headerlink" title="1.3 定义和使用Feign客户端"></a>1.3 定义和使用Feign客户端</h4><p>引入依赖</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--feign客户端依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-openfeign<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>在order-service的启动类添加注解开启Feign的功能：<code>@EnableFeignClients</code></p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213110316047.png" alt="" style="zoom: 33%;" />

<p>编写Feign客户端</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213111042682.png"></p>
<blockquote>
<p><strong>Feign的使用步骤：</strong><br>引入依赖<br>添加@EnableFeignClients注解<br>编写FeignClient接口<br>使用FeignClient中定义的方法代替RestTemplate</p>
</blockquote>
<h3 id="2、自定义配置"><a href="#2、自定义配置" class="headerlink" title="2、自定义配置"></a>2、自定义配置</h3><h4 id="配置Feign日志有两种方式："><a href="#配置Feign日志有两种方式：" class="headerlink" title="配置Feign日志有两种方式："></a>配置Feign日志有两种方式：</h4><p><strong>方式一：配置文件方式</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213225807846.png"></p>
<p><strong>方式二：java代码方式，需要先声明一个Bean：</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213225944483.png"></p>
<h3 id="3、Figen使用优化（性能调优）"><a href="#3、Figen使用优化（性能调优）" class="headerlink" title="3、Figen使用优化（性能调优）"></a>3、Figen使用优化（性能调优）</h3><p><strong>Feign底层的客户端实现：</strong><br><code>URLConnection</code>：默认实现，不支持连接池<br><code>Apache HttpClient </code>：支持连接池<br><code>OKHttp</code>：支持连接池</p>
<p><strong>因此优化Feign的性能主要包括：</strong><br>（1）使用<code>连接池</code>代替默认的URLConnection<br>（2）日志级别，最好用<code>basic</code>或<code>none</code></p>
<h4 id="连接池配置"><a href="#连接池配置" class="headerlink" title="连接池配置"></a>连接池配置</h4><h5 id="Feign添加HttpClient的支持"><a href="#Feign添加HttpClient的支持" class="headerlink" title="Feign添加HttpClient的支持"></a>Feign添加HttpClient的支持</h5><p>引入依赖：</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--引入HttpClient依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>io.github.openfeign<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>feign-httpclient<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>配置连接池：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">feign</span><span class="token punctuation">:</span>
  <span class="token key atrule">client</span><span class="token punctuation">:</span>
    <span class="token key atrule">config</span><span class="token punctuation">:</span>
      <span class="token key atrule">default</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># default全局的配置</span>
        <span class="token key atrule">loggerLevel</span><span class="token punctuation">:</span> BASIC <span class="token comment" spellcheck="true"># 日志级别，BASIC就是基本的请求和响应信息 </span>
  <span class="token key atrule">httpclient</span><span class="token punctuation">:</span>
    <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true </span><span class="token comment" spellcheck="true"># 支持HttpClient的开关</span>
    <span class="token key atrule">max-connections</span><span class="token punctuation">:</span> <span class="token number">200 </span><span class="token comment" spellcheck="true"># 最大连接数</span>
    <span class="token key atrule">max-connections-per-route</span><span class="token punctuation">:</span> <span class="token number">50 </span><span class="token comment" spellcheck="true"># 单个路径的最大连接数</span>
</code></pre>
<blockquote>
<p><strong>Feign的优化：</strong><br><strong>1）日志级别尽量用basic</strong><br><strong>2）使用HttpClient或OKHttp代替URLConnection</strong></p>
<ul>
<li><strong>引入feign-httpClient依赖</strong></li>
<li><strong>配置文件开启httpClient功能，设置连接池参数</strong></li>
</ul>
</blockquote>
<h3 id="4、最佳实践"><a href="#4、最佳实践" class="headerlink" title="4、最佳实践"></a>4、最佳实践</h3><h4 id="Feign的最佳实践"><a href="#Feign的最佳实践" class="headerlink" title="Feign的最佳实践"></a>Feign的最佳实践</h4><blockquote>
<p>所谓继承：让controller和FeignClient继承同一接口<br>所谓抽取：将FeignClient、POJO、Feign的默认配置都定义到一个项目中，供所有消费者使用</p>
</blockquote>
<p>方式一（继承）：给消费者的<code>FeignClient</code>和提供者的<code>controller</code>定义统一的父接口作为标准。</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213232445655.png"></p>
<p>方式二（抽取）：将<code>FeignClient</code>抽取为独立模块，并且把接口有关的<code>POJO</code>、<code>默认的Feign配置</code>都放到这个模块中，提供给所有消费者使用</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213232535909.png"></p>
<h4 id="实现Feign最佳实践"><a href="#实现Feign最佳实践" class="headerlink" title="实现Feign最佳实践"></a>实现Feign最佳实践</h4><p><strong>实现最佳实践方式二的步骤如下：</strong><br>（1）首先创建一个module，命名为<code>feign-api</code>，然后引入<code>feign的starter</code>依赖<br>（2）将order-service中编写的<code>UserClient、User、DefaultFeignConfiguration</code>都复制到feign-api项目中<br>（3）在order-service中引入feign-api的依赖<br>（4）修改order-service中的所有与上述三个组件有关的import部分，改成导入feign-api中的包<br>（5）重启测试</p>
<p><strong>当定义的FeignClient不在SpringBootApplication的扫描包范围时，这些FeignClient无法使用。有两种方式解决：</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213233439656.png"></p>
<h2 id="三、Gateway服务网关"><a href="#三、Gateway服务网关" class="headerlink" title="三、Gateway服务网关"></a>三、Gateway服务网关</h2><h3 id="1、为什么需要网关"><a href="#1、为什么需要网关" class="headerlink" title="1、为什么需要网关"></a>1、为什么需要网关</h3><p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211213234142351.png"></p>
<p><strong>在SpringCloud中网关的实现包括两种：gateway和zuul</strong></p>
<blockquote>
<p><strong>Zuul</strong>是基于<code>Servlet</code>的实现，属于阻塞式编程。</p>
<p>而<strong>SpringCloudGateway</strong>则是基于Spring5中提供的<code>WebFlux</code>，属于<strong>响应式编程</strong>的实现，具备更好的性能。</p>
</blockquote>
<h3 id="2、gateway快速入门"><a href="#2、gateway快速入门" class="headerlink" title="2、gateway快速入门"></a>2、gateway快速入门</h3><h4 id="搭建服务网关"><a href="#搭建服务网关" class="headerlink" title="搭建服务网关"></a>搭建服务网关</h4><p>1）创建新的module，引入SpringCloudGateway的依赖和nacos的服务发现依赖：</p>
<pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true">&lt;!--nacos服务注册发现依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
<span class="token comment" spellcheck="true">&lt;!--网关gateway依赖--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-gateway<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
</code></pre>
<p>2）编写路由配置及nacos地址</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">10010</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> gateway
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> nacos<span class="token punctuation">:</span><span class="token number">8848 </span><span class="token comment" spellcheck="true"># nacos地址</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 网关路由配置</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> user<span class="token punctuation">-</span>service <span class="token comment" spellcheck="true"># 路由id，自定义，必须唯一</span>
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//userservice <span class="token comment" spellcheck="true"># 路由的目标地址  lb就是负载均衡，后面跟服务名称</span>
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 路由断言，也就是判断请求是否符合路由规则的条件</span>
            <span class="token punctuation">-</span> Path=/user/** <span class="token comment" spellcheck="true"># 路径断言，这个是按照路径匹配，判断路径是否是以/user开头，如果是则符合</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> order<span class="token punctuation">-</span>service
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//orderservice
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/order/**
</code></pre>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214000039112.png" alt="" style="zoom: 43%;" />



<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214000341355.png"></p>
<blockquote>
<p>网关搭建步骤：<br>        （1）创建项目，引入nacos服务发现和gateway依赖<br>        （2）配置application.yml，包括服务基本信息、nacos地址、路由<br>路由配置包括：<br>        （1）路由id：路由的唯一标示<br>        （2）路由目标（uri）：路由的目标地址，http代表固定地址，lb代表根据服务名负载均衡<br>        （3）路由断言（predicates）：判断路由的规则，<br>        （4）路由过滤器（filters）：对请求或响应做处理</p>
</blockquote>
<h3 id="3、断言工厂predicates"><a href="#3、断言工厂predicates" class="headerlink" title="3、断言工厂predicates"></a>3、断言工厂predicates</h3><p><strong>predicates：路由断言，判断请求是否符合要求，符合则转发到路由目的地</strong></p>
<blockquote>
<p>我们在配置文件中写的断言规则只是字符串，这些字符串会被Predicate Factory读取并处理，转变为路由判断的条件<br>例如Path=/user/**是按照路径匹配，这个规则是由org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory类来处理的<br>像这样的断言工厂在SpringCloudGateway还有十几个</p>
</blockquote>
<p><strong>Spring提供了11种基本的Predicate工厂：要用哪个去官方查一查文档，看怎么写</strong></p>
<p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring-cloud-gateway/docs/3.1.1-SNAPSHOT/reference/html/#gateway-request-predicates-factories">https://docs.spring.io/spring-cloud-gateway/docs/3.1.1-SNAPSHOT/reference/html/#gateway-request-predicates-factories</a></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214001242768.png"></p>
<blockquote>
<p><code>PredicateFactory</code>的作用是什么？<br>        读取用户定义的断言条件，对请求做出判断<br><code>Path=/user/**</code>是什么含义？<br>        路径是以/user开头的就认为是符合的    </p>
</blockquote>
<p>这一块参考官网进行配置就可以：</p>
<pre class=" language-yaml"><code class="language-yaml"><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> after_route
        <span class="token key atrule">uri</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//example.org
        <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> After=2017<span class="token punctuation">-</span>01<span class="token punctuation">-</span>20T17<span class="token punctuation">:</span>42<span class="token punctuation">:</span>47.789<span class="token punctuation">-</span>07<span class="token punctuation">:</span>00<span class="token punctuation">[</span>America/Denver<span class="token punctuation">]</span>
</code></pre>
<h3 id="4、全局过滤器"><a href="#4、全局过滤器" class="headerlink" title="4、全局过滤器"></a>4、全局过滤器</h3><h4 id="路由过滤器-GatewayFilter"><a href="#路由过滤器-GatewayFilter" class="headerlink" title="路由过滤器 GatewayFilter"></a>路由过滤器 GatewayFilter</h4><p><strong>GatewayFilter是网关中提供的一种过滤器，可以对进入网关的请求和微服务返回的响应做处理：</strong></p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214002520861.png"></p>
<h4 id="过滤器工厂-GatewayFilterFactory"><a href="#过滤器工厂-GatewayFilterFactory" class="headerlink" title="过滤器工厂 GatewayFilterFactory"></a>过滤器工厂 GatewayFilterFactory</h4><p><strong>Spring提供了31种不同的路由过滤器工厂。例如：</strong></p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214002728752.png" alt="" style="zoom: 50%;" />



<h4 id="案例：给所有进入userservice的请求添加一个请求头：Truth-itcast-is-freaking-awesome"><a href="#案例：给所有进入userservice的请求添加一个请求头：Truth-itcast-is-freaking-awesome" class="headerlink" title="案例：给所有进入userservice的请求添加一个请求头：Truth=itcast is freaking awesome!"></a>案例：给所有进入userservice的请求添加一个请求头：Truth=itcast is freaking awesome!</h4><p>实现方式：在gateway中修改application.yml文件，给userservice的路由添加过滤器：</p>
<img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214003315444.png" alt="" style="zoom: 33%;" />



<p><strong>默认过滤器：如果要对所有的路由都生效，则可以将过滤器工厂写到default下。格式如下：</strong></p>
<p><img src="https://notes2021.oss-cn-beijing.aliyuncs.com/2021/image-20211214003458750.png"></p>
<blockquote>
<p><strong>过滤器的作用是什么？</strong><br>        <strong>对路由的请求或响应做加工处理，比如添加请求头</strong><br>        <strong>配置在路由下的过滤器只对当前路由的请求生效</strong><br><strong>defaultFilters的作用是什么？</strong><br>        <strong>对所有路由都生效的过滤器</strong></p>
</blockquote>
<h4 id="全局过滤器-GlobalFilter"><a href="#全局过滤器-GlobalFilter" class="headerlink" title="全局过滤器 GlobalFilter"></a>全局过滤器 GlobalFilter</h4><blockquote>
<p>全局过滤器的作用也是处理一切进入网关的请求和微服务响应，与GatewayFilter的作用一样。<br>区别在于GatewayFilter通过配置定义，处理逻辑是固定的。而GlobalFilter的逻辑需要自己写代码实现。<br>定义方式是实现<code>GlobalFilter</code>接口。</p>
</blockquote>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214005424921.png"></p>
<h4 id="案例：定义全局过滤器，拦截并判断用户身份"><a href="#案例：定义全局过滤器，拦截并判断用户身份" class="headerlink" title="案例：定义全局过滤器，拦截并判断用户身份"></a>案例：定义全局过滤器，拦截并判断用户身份</h4><blockquote>
<p>需求：定义全局过滤器，拦截请求，判断请求的参数是否满足下面条件：<br>（1）参数中是否有authorization<br>（2）authorization参数值是否为admin<br>如果同时满足则放行，否则拦截。</p>
</blockquote>
<p>自定义类，实现<code>GlobalFilter</code>接口，添加<code>@Order</code>注解：</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> cn<span class="token punctuation">.</span>itcast<span class="token punctuation">.</span>gateway<span class="token punctuation">;</span>

<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>GatewayFilterChain<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>GlobalFilter<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>core<span class="token punctuation">.</span>Ordered<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span>HttpStatus<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span>server<span class="token punctuation">.</span>reactive<span class="token punctuation">.</span>ServerHttpRequest<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span>Component<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>util<span class="token punctuation">.</span>MultiValueMap<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>server<span class="token punctuation">.</span>ServerWebExchange<span class="token punctuation">;</span>
<span class="token keyword">import</span> reactor<span class="token punctuation">.</span>core<span class="token punctuation">.</span>publisher<span class="token punctuation">.</span>Mono<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// @Order(-1)</span>
<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AuthorizeFilter</span> <span class="token keyword">implements</span> <span class="token class-name">GlobalFilter</span><span class="token punctuation">,</span> Ordered <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> Mono<span class="token operator">&lt;</span>Void<span class="token operator">></span> <span class="token function">filter</span><span class="token punctuation">(</span>ServerWebExchange exchange<span class="token punctuation">,</span> GatewayFilterChain chain<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// 1.获取请求参数</span>
        ServerHttpRequest request <span class="token operator">=</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        MultiValueMap<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> String<span class="token operator">></span> params <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getQueryParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 2.获取参数中的 authorization 参数</span>
        String auth <span class="token operator">=</span> params<span class="token punctuation">.</span><span class="token function">getFirst</span><span class="token punctuation">(</span><span class="token string">"authorization"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 3.判断参数值是否等于 admin</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>auth<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">// 4.是，放行</span>
            <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">// 5.否，拦截</span>
        <span class="token comment" spellcheck="true">// 5.1.设置状态码</span>
        exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setStatusCode</span><span class="token punctuation">(</span>HttpStatus<span class="token punctuation">.</span>UNAUTHORIZED<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 5.2.拦截请求</span>
        <span class="token keyword">return</span> exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setComplete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214010241143.png"></p>
<blockquote>
<p><strong>全局过滤器的作用是什么？</strong><br>        <strong>对所有路由都生效的过滤器，并且可以自定义处理逻辑。</strong><br><strong>实现全局过滤器的步骤？</strong><br>        <strong>实现GlobalFilter接口</strong><br>        <strong>添加@Order注解或实现Ordered接口</strong><br>        <strong>编写处理逻辑</strong></p>
</blockquote>
<h4 id="过滤器执行顺序"><a href="#过滤器执行顺序" class="headerlink" title="过滤器执行顺序"></a>过滤器执行顺序</h4><p>请求进入网关会碰到三类过滤器：当前路由的过滤器、DefaultFilter、GlobalFilter<br>请求路由后，会将当前路由过滤器和DefaultFilter、GlobalFilter，合并到一个过滤器链（集合）中（设计模式之<code>适配器模式</code>），排序后依次执行每个过滤器</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214010849607.png"></p>
<p>每一个过滤器都必须指定一个int类型的order值，<strong>order值越小，优先级越高，执行顺序越靠前。</strong></p>
<p>GlobalFilter通过实现Ordered接口，或者添加@Order注解来指定order值，由我们自己指定<br>路由过滤器和defaultFilter的order由Spring指定，默认是按照声明顺序从1递增。<br>当过滤器的order值一样时，会按照 <font color=red><strong>defaultFilter &gt; 路由过滤器 &gt; GlobalFilter</strong></font>的顺序执行。</p>
<blockquote>
<p><strong>可以参考下面几个类的源码来查看：</strong></p>
</blockquote>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214011320668.png"></p>
<blockquote>
<p>路由过滤器、defaultFilter、全局过滤器的执行顺序？<br>        order值越小，优先级越高。<br>        当order值一样时，顺序是defaultFilter最先，然后是局部的路由过滤器，最后是全局过滤器。</p>
</blockquote>
<h3 id="5、跨域问题处理"><a href="#5、跨域问题处理" class="headerlink" title="5、跨域问题处理"></a>5、跨域问题处理</h3><p>跨域：域名不一致就是跨域，主要包括：<br>        域名不同： <a target="_blank" rel="noopener" href="http://www.taobao.com/">www.taobao.com</a> 和 <a target="_blank" rel="noopener" href="http://www.taobao.org/">www.taobao.org</a> 和 <a target="_blank" rel="noopener" href="http://www.jd.com/">www.jd.com</a> 和 miaosha.jd.com<br>        域名相同，端口不同：localhost:8080和localhost:8081<br>跨域问题：浏览器禁止请求的发起者与服务端发生跨域<code>ajax</code>请求，请求被浏览器拦截的问题<br>解决方案：CORS</p>
<p>网关处理跨域采用的同样是CORS方案，并且只需要简单配置即可实现：</p>
<p><img src="https://gitee.com/Lemonade19/blog-img/raw/master/img/image-20211214012404184.png"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Lemonade</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://101.42.229.218/2022/01/01/SpringCloud_2%E9%BB%91%E9%A9%AC/">http://101.42.229.218/2022/01/01/SpringCloud_2%E9%BB%91%E9%A9%AC/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Lemonade</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/spring/">
                                    <span class="chip bg-color">spring</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/01/02/html5-css3-%E5%89%8D%E7%AB%AF%E9%A1%B9%E7%9B%AE-%E9%BB%91%E9%A9%AC20211116/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/5.jpg" class="responsive-img" alt="html5&amp;css3&amp;前端项目_黑马20211116">
                        
                        <span class="card-title">html5&amp;css3&amp;前端项目_黑马20211116</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-01-02
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/6-%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/" class="post-category">
                                    6.前端基础
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/html5-css3/">
                        <span class="chip bg-color">html5&amp;css3</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/12/31/hexo%E6%90%AD%E5%BB%BA%E8%AE%B0%E5%BD%95/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="hexo搭建记录">
                        
                        <span class="card-title">hexo搭建记录</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-12-31
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/5-%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/" class="post-category">
                                    5.环境配置
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/hexo/">
                        <span class="chip bg-color">hexo</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="7141772781"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='list'
                   preload='auto'
                   volume='0.7'
                   list-folded='false'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="/libs/aplayer/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2022</span>
            
            <a href="/about" target="_blank">Lemonade</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">55.6k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/cat0501" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://blog.csdn.net/AC_872767407?spm=1001.2014.3001.5343" class="tooltipped" target="_blank" data-tooltip="关注我的CSDN: https://blog.csdn.net/AC_872767407?spm=1001.2014.3001.5343" data-position="top" data-delay="50">
        <i class="fab fa-csdn">C</i>
    </a>



    <a href="mailto:17782975312@163.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=872767407" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 872767407" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

    
    <script type="text/javascript">
        //只在桌面版网页启用特效
        var windowWidth = $(window).width();
        if (windowWidth > 768) {
            document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
        }
    </script>
        

</body>

</html>
